//
//  BSectionController.m
//  BaseProject
//
//  Created by itzhaolei on 2024/5/13.
//

#import "BSectionController.h"
#import "CommonCollectionViewCell.h"
#import "PageListCoordinator.h"

@interface BSectionController()

/// 演示数据变化
@property (nonatomic, unsafe_unretained) NSInteger num;
/// 释放器
@property (nonatomic, weak) RACDisposable *disposable;

@end

@implementation BSectionController

- (void)dealloc {
    // 因为coordinator.reloadBSectionSubject的作用域是超出本类的，但是subscriber是在本类中进行注册的，所以，当本类dealloc时，应该移除subscriber
    // 如果不移除，subscribeNext会依然被call，而且内部的weakSelf会是nil，这些都是因为没移除造成的错误影响。
    [self.disposable dispose];
    NSLog(@"coordinator.reloadBSectionSubject被释放");
}

- (instancetype)initWithCoordinator:(CommonListCoordinator<ListCoordinatorDelegate> *)coordinator {
    if (self = [super initWithCoordinator:coordinator]) {
        NSLog(@"B区行为被初始化");
        [self addActions];
    }
    return self;
}

#pragma mark - Actions
- (void)addActions {
    MJWeakSelf;
    // 刷新所有区
    PageListCoordinator *coordinator = (PageListCoordinator *)self.delegate;
    __weak typeof(coordinator)weakCoordinator = coordinator;
    self.disposable = [coordinator.reloadBSectionSubject subscribeNext:^(id  _Nullable x) {
        weakSelf.num += 1;
        [weakCoordinator reloadSectionController:weakSelf];
    }];
}

#pragma mark - Overwrite

// 当前区域内的单元格所占大小
- (CGSize)sizeForItemAtIndex:(NSInteger)index {
    return CGSizeMake(UIScreen.mainScreen.bounds.size.width / 2, 200);
}

// 当前区域内的单元格构建与复用
- (__kindof UICollectionViewCell *)cellForItemAtIndex:(NSInteger)index {
    CommonCollectionViewCell *cell = [self.collectionContext dequeueReusableCellOfClass:[CommonCollectionViewCell class] forSectionController:self atIndex:index];
    cell.contentLabel.text = [NSString stringWithFormat:@"【B区】演示跨区刷新:\n%ld", self.num];
    return cell;
}

@end
